<?php

/**
 * 
 * @param type $openid
 * @param type $title
 * @param type $content
 * @param type $uri
 * @param type $state
 * @param type $image
 * @return type
 */
function WeNewsMessage($openid, $title, $content, $uri, $state, $image) {
    
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".access_token();
        $post = array(
                "touser" => $openid,
                "msgtype" => "news",
                "news" => array(
                        "articles" => array(
                                array(
                                'title'=> $title,
                                'description' => $content,
                                //'url' => 'http://'.$_SERVER['HTTP_HOST'].U('message/read?id='.$id),
                                'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('appid').'&redirect_uri=http://'.$_SERVER['SERVER_NAME'].$uri.'&response_type=code&scope=snsapi_base&state='.$state.'&connect_redirect=1#wechat_redirect',
                                'picurl' => 'http://'.$_SERVER['HTTP_HOST'].$image,
                                ),
                        ),
                ),
        );
        $data = json_encode2($post);
        $ay = curl($url,$data);
        $r['date'] = $date;
        $r['post'] = $post;
        $r['json'] = $data;
        $r['state'] = json_decode($ay,true);
        return $r;
}

	
function WeSend($openid,$content){
	$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".access_token();
	$post = array(
		"touser" => $openid,
		"msgtype" => "text",
		"text" => array(
			"content" => $content,
		),
	);
	$data = json_encode2($post);
	$ay = curl($url,$data);
	return $ay;
}

function check_wap() {  
    if (isset($_SERVER['HTTP_VIA'])) return true;  
    if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;  
    if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;  
    if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {  
        // Check whether the browser/gateway says it accepts WML.  
        $br = "WML";  
    } else {  
        $browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';  
        if(empty($browser)) return true;
        $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');  
              
        $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');  
              
        $found_mobile=checkSubstrs($mobile_os_list,$browser) ||  
                  checkSubstrs($mobile_token_list,$browser); 
    if($found_mobile)
      $br ="WML";
    else $br = "WWW";
    }  
    if($br == "WML") {  
        return true;  
    } else {  
        return false;  
    }  
}

function checkSubstrs($list,$str){
    $flag = false;
    for($i=0;$i<count($list);$i++){
      if(strpos($str,$list[$i]) > 0){
        $flag = true;
        break;
      }
    }
    return $flag;
}

function getCurrentLawyer($uid) {
    $res = M('lawyer')
         ->alias("l")
         ->join("LEFT JOIN l_user as u on l.uid = u.uid")
         ->where("l.uid = ".$uid)
         ->find();

    return $res;
}

function getCurrentTimeType() {
    $hour = date('H');
    
    $type = 0;
    
    if($hour >= 8 && $hour < 12) {
        $type = 1;
    } else if($hour >= 12 && $hour < 18) {
        $type = 2;
    } else if($hour >= 18 && $hour <= 22) {
        $type = 3;
    }
    
    return $type;
}

function checkCurrentLawyerWorkingStatus($lawyer) {
    
    $currentStatus = 0;
    
    $type = getCurrentTimeType();
    
    switch ($type) {
        case 0:
            
            $currentStatus = 0;
            
            break;

        case 1:
            
            if($lawyer['morning'] == 1)
                $currentStatus = 1;
            
            break;
            
        case 2:
            
            if($lawyer['afternoon'] == 1)
                $currentStatus = 1;
            
            break;    
        
        case 3:
            
            if($lawyer['night'] == 1)
                $currentStatus = 1;
            
            break; 
            
        default:
            break;    
            
    }

    return $currentStatus;
        
    
}

//a为子集 ，b为整集
function isContainArray($a, $b) {
    
    $c = array_diff($a, $b);
    $flag = empty($c)?1 : 0;
    
    return $flag;
}

/* user Id */
function changeId($type,$uid){

    $base = (int)10000;

    $id = $base + $uid;

    switch($type){

        case "1":
            $id = "D".$id;
            break;

        case "2":
            $id = "P".$id;
            break;

        case "3":
            $id = "F".$id;
            break;

    }

    return $id;

}

function isNullOrEmpty($obj) {
    return (!isset($obj) || empty($obj) || $obj == null);
}

function getCurrentDate() {
    return date('Y-m-d H:i:s');
}
function getEnumString($enum, $value) {

    $key = array_search($value, $enum);

    $strKey = $key . '_STRING';

    return $enum[$strKey];
}



